package com.sky.sd.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctc.wstx.util.StringUtil;
import com.sky.sd.common.exception.ServiceException;
import com.sky.sd.system.bean.dto.DicNode;
import com.sky.sd.system.bean.po.Dic;
import com.sky.sd.system.bean.vo.DeleteVo;
import com.sky.sd.system.bean.vo.DicAddVo;
import com.sky.sd.system.bean.vo.DicSearchVo;
import com.sky.sd.system.bean.vo.DicUpdateVo;
import com.sky.sd.system.constant.SystemConstant;
import com.sky.sd.system.dao.DicMapper;
import com.sky.sd.system.enums.DicType;
import com.sky.sd.system.enums.IsDefaultType;
import com.sky.sd.system.service.IDicService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jcajce.provider.symmetric.AES;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author 弓振
 * @since 2020-07-06
 */
@Service
@Slf4j
public class DicServiceImpl extends ServiceImpl<DicMapper, Dic> implements IDicService {

    @Override
    public IPage<DicNode> listPDicPage(DicSearchVo vo) {
        IPage<DicNode> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        IPage<DicNode> dicNodePage = baseMapper.listPDicPage(page, vo);
        // 查询子项
        dicNodePage.getRecords().parallelStream().forEach(dicNode -> {
            List<Dic> childDics = baseMapper.selectList(Wrappers.<Dic>lambdaQuery()
                    .eq(Dic::getParentId,dicNode.getId()).orderByDesc(Dic::getIsDefault).orderByAsc(Dic::getSort));
            dicNode.setChildren(childDics);
        });
        return dicNodePage;
    }

    @Override
    public void add(DicAddVo vo) {
        canUpdate(vo.getParentId(), null, vo.getCode(), vo.getIsDefault());

        Dic dic = new Dic();
        BeanUtils.copyProperties(vo, dic);
        baseMapper.insert(dic);
    }

    @Override
    public void update(DicUpdateVo vo) {
        canUpdate(vo.getParentId(), vo.getId(), vo.getCode(), vo.getIsDefault());

        Dic dic = new Dic();
        BeanUtils.copyProperties(vo, dic);
        baseMapper.updateById(dic);
    }

    @Override
    public void delete(DeleteVo vo) {
        Dic dic = baseMapper.selectById(vo.getIds().iterator().next());
        if(DicType.SYSTEM.equals(dic.getType())) {
            throw new ServiceException("系统级数据字典不能删除");
        }
        baseMapper.deleteBatchIds(vo.getIds());
        vo.getIds().parallelStream().forEach(id -> {
            baseMapper.delete(Wrappers.<Dic>lambdaQuery().eq(Dic::getParentId, id));
        });
    }

    private void canUpdate(String parentId, String id, String code, Integer isDefault) {

        // 判断编码是否存在
        int codeCnt = baseMapper.selectCount(Wrappers.<Dic>lambdaQuery().ne(StringUtils.isNoneBlank(id),Dic::getId, id)
                .eq(Dic::getCode, code)
                .eq(StringUtils.isNotBlank(parentId) && !SystemConstant.TOP_PARENT_ID.equals(parentId),Dic::getParentId, parentId));
        if(codeCnt > 0) {
            throw new ServiceException("字典编码已存在");
        }

        // 判断是否已经存在默认项
        if(IsDefaultType.YES.getType().equals(isDefault)) {
            int defaultCnt = baseMapper.selectCount(Wrappers.<Dic>lambdaQuery().ne(StringUtils.isNoneBlank(id),Dic::getId, id)
                    .eq(Dic::getIsDefault, IsDefaultType.YES.getType()).eq(Dic::getParentId, parentId));

            if(defaultCnt > 0) {
                throw new ServiceException("字典项"+code+"默认值已存在");
            }
        }
    }
}
